package com.bnu.employment.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnu.employment.Entity.Employment;
import com.bnu.employment.Mapper.EmploymentMapper;
import com.bnu.employment.Service.EmploymentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Service
@Transactional
public class EmploymentServiceImpl extends ServiceImpl<EmploymentMapper,Employment> implements EmploymentService {

    @Resource
    private EmploymentService employmentService;

    @Resource
	private EmploymentMapper employmentMapper;

    @Override
    public Object findAllByEtype(HttpServletRequest request) {
        //获取前台发送过来的数据
        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String eType = String.valueOf(request.getParameter("e_type"));
        //使用分页插件
        IPage<Employment> page = new Page<>(pageNo, pageSize);
        //使用条件构造器
        QueryWrapper<Employment> wrapper = new QueryWrapper<>();
        //添加条件
        wrapper.eq(StringUtils.isNotBlank(eType),"e_type",eType)
                .orderByAsc("eId");
        //返回service层中的page()，向其中传入页面信息和查询条件
        return employmentService.page(page,wrapper);
    }
    
    @Override
    public Object findEmploymentByWrapper(HttpServletRequest request) {
        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        String EType = String.valueOf(request.getParameter("e_type"));
        String pa = String.valueOf(request.getParameter("pa"));
        String majorType = String.valueOf(request.getParameter("major_type"));
        String officeLocation = String.valueOf(request.getParameter("office_location"));

        IPage<Employment> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Employment> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(EType),"e_type",EType)
                .eq(StringUtils.isNotBlank(majorType),"major_type",majorType)
                .eq(StringUtils.isNotBlank(officeLocation),"office_location",officeLocation)
                .orderByAsc("eId");
        //pa是输入框接受的值，可以用来配合选择框进行搜索，也可以不填写，pa会从"company"、"location"、"office_location"、"position"中进行查询
        if (StringUtils.isNotBlank(pa)){
            wrapper.and(i -> i.like("company", pa).or().like("location", pa).or().like("office_location", pa).or().like("position", pa));
        }
        return employmentService.page(page,wrapper);
    }
    
    @Override
    public Object deleteEmploymentByEid(HttpServletRequest request) {
    	   Integer eid = Integer.valueOf(request.getParameter("eId"));
           return employmentMapper.deleteById(eid);
    }
    
    @Override
    public Object insertEmploymentByEid(HttpServletRequest request) {
    	   Employment employment = new Employment();
    	   employment.setEid(Integer.valueOf(request.getParameter("eId")));
    	   employment.setCompany(String.valueOf(request.getParameter("company")));
    	   employment.setLocation(String.valueOf(request.getParameter("location")));
    	   employment.setOfficeLocation(String.valueOf(request.getParameter("office_location")));
    	   employment.setPosition(String.valueOf(request.getParameter("position")));
    	   employment.setSalary(String.valueOf(request.getParameter("salary")));
    	   employment.setRequirements(String.valueOf(request.getParameter("requirements")));
    	   employment.setDuties(String.valueOf(request.getParameter("duties")));
    	   employment.setBenefits(String.valueOf(request.getParameter("benefits")));
    	   employment.setResumeName(String.valueOf(request.getParameter("resume_name")));
    	   employment.setContact(String.valueOf(request.getParameter("contact")));
    	   Date now = new Date();
    	   employment.setPostTime(now);
    	   employment.setTag(Integer.valueOf(request.getParameter("tag")));
    	   employment.setEType(Integer.valueOf(request.getParameter("e_type")));
    	   employment.setMajorType(String.valueOf(request.getParameter("major_type")));
           return employmentMapper.insert(employment);
    }

    @Override
    public Object updateEmploymentByEid(HttpServletRequest request) {
    	Integer eid = Integer.valueOf(request.getParameter("eId"));
    	UpdateWrapper<Employment> updateWrapper = new UpdateWrapper<Employment>();
        updateWrapper.eq("eId",eid);
    	Employment employment =new Employment();
    	employment.setTag(Integer.valueOf(request.getParameter("tag")));
    	return employmentService.update(employment,updateWrapper);
   }	
    
    @Override
    public Object updateEmploymentAll(HttpServletRequest request) {
    	Integer eid = Integer.valueOf(request.getParameter("eId"));
    	String salary = String.valueOf(request.getParameter("salary"));
    	String requirements = String.valueOf(request.getParameter("requirements"));
    	String duties = String.valueOf(request.getParameter("duties"));
    	String benefits = String.valueOf(request.getParameter("benefits"));
    	String tag = String.valueOf(request.getParameter("tag"));
    	UpdateWrapper<Employment> updateWrapper = new UpdateWrapper<Employment>();
        updateWrapper.eq("eId",eid);
    	Employment employment =new Employment();
    	if(StringUtils.isNotBlank(salary)){
    		employment.setSalary(salary);
    	}
    	if(StringUtils.isNotBlank(requirements)){
    		employment.setRequirements(requirements);
    	}
    	if(StringUtils.isNotBlank(duties)){
    		employment.setDuties(duties);
    	}
    	if(StringUtils.isNotBlank(benefits)){
    		employment.setBenefits(benefits);
    	}
    	if(StringUtils.isNotBlank(tag)){
    		employment.setTag(Integer.valueOf(request.getParameter("tag")));
    	}
    	return employmentService.update(employment,updateWrapper);
    }

}
    


